package com.tencent.crazypenguin.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.opensymphony.xwork2.ActionSupport;
import com.tencent.crazypenguin.bean.Comment;
import com.tencent.crazypenguin.bean.User;
import com.tencent.crazypenguin.config.HbmUtil;
import com.tencent.crazypenguin.config.ResultType;
import com.tencent.crazypenguin.dao.CommentDao;
import com.tencent.crazypenguin.dao.UserDao;
import com.tencent.crazypenguin.util.JsonUtil;

import net.sf.json.JSONObject;

public class Comments extends ActionSupport {
	/**
	 * by zhm
	 */
	private static final long serialVersionUID = -1660704502356077315L;
	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String execute() throws Exception {

		// 获取输入数据
		JSONObject authData = HbmUtil.getAuthDate();
		JSONObject rspData = new JSONObject();
		CommentDao cmDao = new CommentDao();
		UserDao userDao = new UserDao();
		long comment = authData.getLong("comments");
		long userId = authData.getLong("userId");
		User user = userDao.findById(userId, new User());
		Date pTime = user.getP_time();
	    
//		//test01--
//		String loginName = "user44";
//		String comment = "69";
//		//--test01

		// 插入评分数据
		Session s = HbmUtil.getSession();

		try {

			Transaction trans = s.beginTransaction();
			
			String hqlUpdate = "from Comment where id_docom=:id_docom and time_go=:time_go";
           
			Query query = s.createQuery(hqlUpdate);
			
			query.setParameter("id_docom", userId);
			query.setParameter("time_go", pTime);
			
			Comment cm = (Comment) query.getSingleResult();
			cm.setStar_com(comment);
			
			cmDao.update(cm);
			
			trans.commit();
			s.close();
			rspData.put("resultCode",ResultType.SUCEESS);
			result = rspData.toString();
			return "done";
			
		} catch (Exception e) {
			e.printStackTrace();
			rspData.put("resultCode",ResultType.EXCEPTION);
			result = rspData.toString();
			return "done";
		}
	}

	@SuppressWarnings("rawtypes")
	public long getUserID(String loginName) {

		long loginID = 0;
		Session s = HbmUtil.getSession();
		s.beginTransaction();

		Query query = s.createQuery("SELECT id FROM User WHERE user = ?");
		query.setParameter(0, loginName);
		
		loginID = (long)query.getSingleResult();
		s.getTransaction().commit();
		s.close();

		return loginID;
	}
	
	@SuppressWarnings("rawtypes")
	public Timestamp getTimeGo(String loginName) {

		Timestamp timeGo = null;
		Session s = HbmUtil.getSession();
		s.beginTransaction();
		
		Query query = s.createQuery("SELECT p_time FROM User WHERE user = ?");
		query.setParameter(0, loginName);
		
		timeGo = (Timestamp)query.getSingleResult();
		s.getTransaction().commit();
		s.close();
		return timeGo;
	}

}
